package com.huawei.appmarket.service.distribution.net.view;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.huawei.appgallery.downloadengine.api.SessionDownloadTask;
import com.huawei.appgallery.foundation.ui.framework.activity.BaseActivity;
import com.huawei.appgallery.foundation.ui.framework.widget.dialog.dialogactivity.DialogActivity;
import com.huawei.appmarket.R;
import com.huawei.secure.android.common.intent.SafeBroadcastReceiver;
import java.util.ArrayList;
import java.util.List;
import o.ckh.a;
import o.Logger;
import o.doo;
import o.dph;
import o.dpi;
import o.dvr;
import o.dvt;
import o.dwb;
import o.dxq;
import o.CollectCheck;
import o.ActivityUtil;
import o.evd;
import o.evk;

public class DistributionNetActivity extends BaseActivity<DistributionNetActivityProtocol> implements a, OnDismissListener {
    private List<String> ˊॱ = new ArrayList();
    private BroadcastReceiver ˋॱ = new SafeBroadcastReceiver(this) {
        final /* synthetic */ DistributionNetActivity ˊ;

        {
            this.ˊ = r1;
        }

        public void onReceiveMsg(Context context, Intent intent) {
            if ("android.net.conn.CONNECTIVITY_CHANGE".equals(intent.getAction())) {
                NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra("networkInfo");
                if (networkInfo != null && State.CONNECTED == networkInfo.getState()) {
                    int ॱ = dvt.ॱ(context);
                    if (ॱ == 0 || 1 == ॱ) {
                        Logger.ˎ("DistributionNetActivity", "Keep current download dialog");
                        return;
                    }
                    Logger.ˎ("DistributionNetActivity", "network has changed,close the download dialog, new net type=" + ॱ);
                    this.ˊ.finish();
                }
            }
        }
    };
    private List<String> ˏॱ = new ArrayList();

    enum c {
        RESUME_TASK,
        START_TASK,
        RESERVE_TASK
    }

    static {
        dpi.ˎ(DistributionNetActivity.class);
    }

    public void onCreate(@Nullable Bundle bundle) {
        requestWindowFeature(1);
        super.onCreate(bundle);
        evd.ˏ(getWindow());
        DistributionNetActivityProtocol distributionNetActivityProtocol = (DistributionNetActivityProtocol) ʾ();
        if (distributionNetActivityProtocol == null || distributionNetActivityProtocol.ˏ() == null) {
            Logger.e("DistributionNetActivity", "protocol is null!");
            finish();
            return;
        }
        this.ˏॱ = distributionNetActivityProtocol.ˏ().ˎ();
        this.ˊॱ = distributionNetActivityProtocol.ˏ().ˋ();
        long ˊ = distributionNetActivityProtocol.ˏ().ˊ();
        if (doo.ʻ(this)) {
            dvr.ॱ(this, ˊ, this, this, false);
            ᐝ();
            return;
        }
        Logger.ˎ("DistributionNetActivity", "can start mobile activity because not in mobile net!");
        ͺ();
        finish();
    }

    private void ᐝ() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        ActivityUtil.registerReceiver(this, intentFilter, this.ˋॱ);
    }

    public void onDestroy() {
        ActivityUtil.unregisterReceiver(this, this.ˋॱ);
        super.onDestroy();
    }

    public void ˏ(AlertDialog alertDialog, DialogActivity.a aVar, int i) {
        switch (i) {
            case -2:
                ॱˊ();
                break;
            case -1:
                ͺ();
                break;
        }
        finish();
    }

    private void ॱˊ() {
        if (!ᐝॱ()) {
            String ʿ;
            int i = 0;
            String str = "";
            if (!CollectCheck.isEmpty(this.ˏॱ)) {
                for (String ʿ2 : this.ˏॱ) {
                    int i2;
                    SessionDownloadTask ˋ = dvt.ˏ().ˋ(ʿ2);
                    if (ˋ != null) {
                        i2 = i + 1;
                        ʿ2 = ˋ.ʿ();
                        ˊ(ˋ, c.RESERVE_TASK);
                    } else {
                        ʿ2 = str;
                        i2 = i;
                    }
                    i = i2;
                    str = ʿ2;
                }
            }
            if (!CollectCheck.isEmpty(this.ˊॱ)) {
                for (String ʿ22 : this.ˊॱ) {
                    SessionDownloadTask ॱ = dph.ˋ().ॱ(ʿ22);
                    if (ॱ != null) {
                        i++;
                        str = ॱ.ʿ();
                        ˊ(ॱ, c.RESERVE_TASK);
                    }
                }
            }
            if (i > 1) {
                dwb.ˎ(i);
            } else {
                dwb.ˋ(str);
            }
        }
    }

    private void ͺ() {
        if (!ᐝॱ()) {
            SessionDownloadTask ˋ;
            if (!CollectCheck.isEmpty(this.ˏॱ)) {
                for (String ˋ2 : this.ˏॱ) {
                    ˋ = dvt.ˏ().ˋ(ˋ2);
                    if (ˋ != null) {
                        ˊ(ˋ, c.RESUME_TASK);
                    }
                }
            }
            if (!CollectCheck.isEmpty(this.ˊॱ)) {
                for (String ˋ22 : this.ˊॱ) {
                    ˋ = dph.ˋ().ॱ(ˋ22);
                    if (ˋ != null) {
                        ˊ(ˋ, c.START_TASK);
                    }
                }
            }
        }
    }

    private boolean ᐝॱ() {
        if (doo.ˊ(this)) {
            return false;
        }
        evk.ॱ(getResources().getString(R.string.no_available_network_prompt_toast), 0).ˋ();
        ʼॱ();
        return true;
    }

    public void onDismiss(DialogInterface dialogInterface) {
        ʼॱ();
        finish();
    }

    private void ʼॱ() {
        if (!CollectCheck.isEmpty(this.ˊॱ)) {
            for (String ॱ : this.ˊॱ) {
                SessionDownloadTask ॱ2 = dph.ˋ().ॱ(ॱ);
                if (ॱ2 != null) {
                    dxq.ˎ(ॱ2, this);
                }
            }
        }
    }

    private void ˊ(@NonNull SessionDownloadTask sessionDownloadTask, c cVar) {
        sessionDownloadTask.ˋॱ(8);
        switch (cVar) {
            case START_TASK:
                dvt.ˏ().ॱ(sessionDownloadTask, false);
                return;
            case RESUME_TASK:
                dvt.ˏ().ˊ(sessionDownloadTask.ʾ());
                return;
            case RESERVE_TASK:
                dvt.ˏ().ʻ(sessionDownloadTask);
                return;
            default:
                return;
        }
    }

    public void ᐝᐝ() {
    }
}
